/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stigonacci;

/**
 *
 * @author ronnevig
 */

public class Stigonacci implements Runnable {

	private int n;
	private int result;

	public static void main(String[] args) throws InterruptedException {
		
		Stigonacci s = new Stigonacci(10);
		Thread t = new Thread(s);
		t.start();
		t.join(); // join venter til tråden er ferdig
		System.out.println(s.getValue());
	}
	
	public Stigonacci(int n) {
		this.n = n;
	}

	@Override
	public void run() {
		
		
		// Definisjon i følge Wikipedia
		// http://no.wikipedia.org/wiki/Fibonaccitall

		if (n == 0) {
			result = 0;

		}

		if (n <= 2) {
			result = 1;

		}

		else {

			Stigonacci s1 = new Stigonacci(n - 1);

			Stigonacci s2 = new Stigonacci(n - 2);

			Thread t1 = new Thread(s1);
			Thread t2 = new Thread(s2);

			t1.start();
			t2.start();

			try {
				t1.join();
				t2.join();
				// join venter til tråden er ferdig
				// http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html

			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			result = s1.getValue() + s2.getValue();

		}

	}

	public int getValue() {
		return result;
	}

	public String ping() {

		return "ping";
	}

}
